#delimit;
clear all;

use "Sen_Est.dta", replace;

gen incumbent_tab = incumbentdem;
label variable incumbent_tab "Incumbent";

eststo: xtreg est_sen_dem incumbent_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

drop incumbent_tab;
gen incumbent_tab = incumbentrep;
label variable incumbent_tab "Incumbent";

eststo: xtreg est_sen_rep incumbent_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

esttab using Sen_Est_Regressions.tex, mlabels("Democratic Cands." "Republican Cands.") note("Heteroskedasticity robust standard errors clustered at state level") keep(incumbent_tab _cons) label se star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars("Year_FE Year Fixed Effects" "District_FE State Fixed Effects")  title(LPM for Estimate of Ideology (Senate Elections)) replace ;
eststo clear;

#delimit;
set seed 339487731;
gen p_Voter_Sen_Rep_Ideol  =  runiform() -0.5;
replace p_Voter_Sen_Rep_Ideol = Voter_Sen_Rep_Ideol if Voter_Sen_Rep_Ideol != .;

gen p_Voter_Sen_Dem_Ideol  =  runiform() -0.5;
replace p_Voter_Sen_Dem_Ideol = Voter_Sen_Dem_Ideol  if Voter_Sen_Dem_Ideol != .;

egen sd_voter_sen_rep= sd(p_Voter_Sen_Rep_Ideol), by(state_year);
egen sd_voter_sen_dem= sd(p_Voter_Sen_Dem_Ideol), by(state_year);

duplicates drop state_year, force;

drop incumbent_tab;
gen incumbent_tab = incumbentdem;
label variable incumbent_tab "Incumbent";

eststo: xtreg sd_voter_sen_dem incumbent_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

drop incumbent_tab;
gen incumbent_tab = incumbentrep;
label variable incumbent_tab "Incumbent";

eststo: xtreg sd_voter_sen_rep incumbent_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

esttab using Sen_SD_Regressions.tex, mlabels("Democratic Cands." "Republican Cands.") note("Heteroskedasticity robust standard errors clustered at state level") keep(incumbent_tab _cons) label se star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars("Year_FE Year Fixed Effects" "District_FE State Fixed Effects")  title(Standard Deviation of Ideology Estimates within Candidate-State-Year (Senate Elections)) replace ;
eststo clear;


#delimit;
use "House_Est.dta", replace;

gen incumbent_tab = incumbentdem;
gen expchall_tab = expchalldem;

label variable incumbent_tab "Incumbent";
label variable expchall_tab "Exp. Chall";

eststo: xtreg est_house_dem incumbent_tab expchall_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

drop incumbent_tab expchall_tab;
gen incumbent_tab = incumbentrep;
gen expchall_tab = expchallrep;

label variable incumbent_tab "Incumbent";
label variable expchall_tab "Exp. Chall";

eststo: xtreg est_house_rep incumbent_tab expchall_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";
esttab using House_Est_Regressions.tex, mlabels("Democratic Cands." "Republican Cands.") note("Heteroskedasticity robust standard errors clustered at district level")  keep(incumbent_tab expchall_tab _cons) label se star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars("Year_FE Year Fixed Effects" "District_FE District Fixed Effects")  title(LPM for Estimate of Ideology (House Elections)) replace; 
eststo clear;

xtreg est_house_dem incumbentdem expchalldem i.year if year < 2012, fe robust;
xtreg est_house_rep incumbentrep expchallrep i.year if year < 2012, fe robust;

#delimit;
gen p_Voter_House_Rep_Ideol  =  runiform() -0.5;
replace p_Voter_House_Rep_Ideol = Voter_House_Rep_Ideol if Voter_House_Rep_Ideol != .;

gen p_Voter_House_Dem_Ideol  =  runiform() -0.5;
replace p_Voter_House_Dem_Ideol = Voter_House_Dem_Ideol  if Voter_House_Dem_Ideol != .;

egen dist_year = concat(distidrunfor year);

egen sd_voter_house_rep= sd(p_Voter_House_Rep_Ideol), by(dist_year);
egen sd_voter_house_dem= sd(p_Voter_House_Dem_Ideol), by(dist_year);

duplicates drop dist_year, force;


drop incumbent_tab expchall_tab;
gen incumbent_tab = incumbentdem;
gen expchall_tab = expchalldem;

label variable incumbent_tab "Incumbent";
label variable expchall_tab "Exp. Chall";

eststo: xtreg sd_voter_house_dem incumbent_tab expchall_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

drop incumbent_tab expchall_tab;
gen incumbent_tab = incumbentrep;
gen expchall_tab = expchallrep;

label variable incumbent_tab "Incumbent";
label variable expchall_tab "Exp. Chall";

eststo: xtreg sd_voter_house_rep incumbent_tab expchall_tab i.year, fe robust;
estadd local Year_FE "Yes";
estadd local District_FE "Yes";

esttab using House_SD_Regressions.tex, mlabels("Democratic Cands." "Republican Cands.") note("Heteroskedasticity robust standard errors clustered at state level") keep(incumbent_tab expchall_tab _cons) label se star(+ 0.10 * 0.05 ** 0.01 *** 0.001) scalars("Year_FE Year Fixed Effects" "District_FE District Fixed Effects")  title(Standard Deviation of Ideology Estimates within Candidate-District-Year (House Elections)) replace ;
eststo clear;
